package game.source.ships;

import game.source.gui.ship.GuiShip;
import game.source.ships.GameInterface.PriceType;
import game.source.shot.GuiShotInterface.Level;
import game.source.shot.GuiShotInterface.Shooter;
import game.source.util.Timer;
import game.source.util.TimerListener;
import game.source.util.TrackerListener;

import java.awt.Point;
import java.util.Random;

/**
 *EnemyShip:
 * 	Realiza o controle da movimentação e das ações da nave inimiga.
 */
public abstract class EnemyShip extends Ship implements TrackerListener,TimerListener{
	private Point			playerPos;
	private Point			start;
	private Timer			timer;
	private	boolean 		explosion;
	public EnemyShip(Point start, GuiShip gui,GameInterface game,double velx, double vely){
		super(5,0,start,gui,game,velx,vely);
		this.playerPos	= new Point();
		this.start		= new Point(start);
		this.timer		= new Timer(this,500);
		this.explosion	= false;
		this.timer.start();
	}
	protected void superMove(Point p){
		super.move(p);
	}
	@Override
	public void move(Point p){
		if(this.start.x > game.getGameWidth()/2){
			if(getPosX() >= this.start.x){				
				super.move(new Point(game.getGameWidth()-this.start.x,game.getGameHeight()+getHeight()));
			}else if(getPosX() <= game.getGameWidth()-this.start.x){
				super.move(new Point(this.start.x,game.getGameWidth()+getHeight()));
			}
		}else{
			if(getPosX() >= game.getGameWidth()-this.start.x){				
				super.move(new Point(this.start.x,game.getGameHeight()+getHeight()));
			}else if(getPosX() <= this.start.x){
				super.move(new Point(game.getGameWidth()-this.start.x,game.getGameHeight()+getHeight()));
			}
		}
		this.playerPos.x = p.x;
		this.playerPos.y = p.y;
	}
	@Override
	public void reciveFire(Level level){
		if(!this.explosion){
			if(this.helthPoints <= 0){
				super.reciveFire(level);
				Random rand = new Random();
				int value = rand.nextInt(10);
				if(value >= 3 && value <= 6){
					this.game.createPrice(this.getPosition(),PriceType.LevelUp);
				}else if(value > 6){
					this.game.createPrice(this.getPosition(),PriceType.NewLife);
				}
				this.timer.end();
				this.gui.explosion();
				this.explosion = true;
			}
		}
	}
	@Override
	public void update(){
		this.game.createShot(this, Shooter.ENEMY, Level.BASIC);		
	}
	@Override
	public void endExplosion(){
		this.game.removeEnemy(this);
	}
	@Override
	public void start(){
		super.start();
		this.timer.start();
	}
	@Override
	public void play(){
		super.play();
		this.timer.play();
	}
	@Override
	public void pause(){
		super.pause();
		this.timer.pause();
	}
	@Override
	public void end(){
		super.end();
		this.timer.end();
	}
	@Override
	public boolean isPaused(){
		return this.timer.isPaused();
	}
}
